
const Koa=require('koa');
const app=new Koa();
const router=require('koa-router')();
const bodyparser=require('koa-bodyparser');
const query=require('./query')

router.get('/app/list',async ctx=>{
    const res=await query(`select * from list`)
    ctx.body={
        code:1,
        msg:'成功获取用户列表',
        res
    }
})


 router.post('/app/post',async ctx=>{
     const {username,password}=ctx.request.body
     const res=await query(`select * from list where username=? and password=?`,[username,password])
     
     if(res.length){
         ctx.body={
         code:1,
         msg:'数据错误',
     }
     }else{
         const res=await query(`insert into list (username,password) values (?,?)`,[username,password])
            ctx.body={
                code:1,
                res,
                msg:'添加成功'
            }
     
        }
 })

  router.put('/app/put',async ctx=>{
      const {username,password,id}=ctx.request.body;
      const res=await query(`update list set username=?,password=? where id=?`,[username,password,id])
      ctx.body={
          code:1,
          msg:"更新成功"

      }
  })

  router.delete('/app/del',async ctx=>{
      const {id}=ctx.query;
      const res=await query(`delete from list where id=?`,[id]);
      if(res.affectedRows>0){
           ctx.body={
          code:1,
          msg:'删除成功'
      }
      }
     
  })


app.use(bodyparser())
app.use(router.routes())
app.listen(3000,()=>{
    console.log('server runing st http://localhost:3000');
})
